/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.crud.comment;

import javax.swing.JDialog;
import systole.domain.report.template.Comment;
import systole.exceptions.ExceptionDAO;
import systole.utils.ImageUtils;
import systole.view.crud.controller.ControllerEntityEdition;
import systole.view.messages.EntityDescriptions;

/**
 *
 * @author jmj
 */
public class ControllerCommentEdition extends ControllerEntityEdition {

    /**
     *  
     */
    public ControllerCommentEdition() {
        super();
        this.entityName =EntityDescriptions.COMMENT;
    }

    @Override
    protected void loadEntityOnForm() {
        Comment commentToLoad = (Comment) this.curretnEntity;
        JDialogComment formToLoad = (JDialogComment) this.editForm;

        formToLoad.getjEdtName().setText(commentToLoad.getName());
        formToLoad.getjEdtComment().setText(commentToLoad.getText());
        formToLoad.getjChkFemale().setSelected(commentToLoad.isFemale());
        formToLoad.getjChkMale().setSelected(commentToLoad.isMale());
    }

    @Override
    protected void loadEntityFromForm() {
        Comment commentToLoad = (Comment) this.curretnEntity;
        JDialogComment formToLoad = (JDialogComment) this.editForm;

        commentToLoad.setName(formToLoad.getjEdtName().getText());
        commentToLoad.setText(formToLoad.getjEdtComment().getText());
        commentToLoad.setFemale(formToLoad.getjChkFemale().isSelected());
        commentToLoad.setMale(formToLoad.getjChkMale().isSelected());

    }

    @Override
    protected String valid() {
        JDialogComment formToLoad = (JDialogComment) this.editForm;
        Comment comment = (Comment) this.curretnEntity;

        if ((formToLoad.getjEdtName().getText() == null) || (formToLoad.getjEdtName().getText().isEmpty())) {
            return "Debe ingresar el nombre del comentario";
        }

        try {
            if (this.facadeDB.getCommentBroker().existComment(formToLoad.getjEdtName().getText(), comment.getId())) {
                return "Ya existe un comentario con el nombre ingresado";
            }
        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            return ex.getMessage();
        }

        if ((formToLoad.getjEdtComment().getText() == null) || (formToLoad.getjEdtComment().getText().isEmpty())) {
            return "Debe ingresar el comentario";
        }

        return null;
    }  

    @Override
    protected void save() throws ExceptionDAO {
            if (this.isEditing()) {
                this.facadeDB.getCommentBroker().update((Comment) this.curretnEntity);
            } else {
                this.facadeDB.getCommentBroker().insert((Comment) this.curretnEntity);
            }
    }

    @Override
    protected void loadIconOnForm() {
        this.editForm.setIconImage(ImageUtils.buildImage("resources/icons/comment/comment16.png"));
    }

    @Override
    protected Object createEntity() {
        return new Comment();
    }

    @Override
    protected JDialog createEditionForm(java.awt.Frame parent) {
        return new JDialogComment(parent, this);
    }
}
